
/*
   tsAPI Distribution
   zoom_image Class

   TrueSpectra Inc, 2002
   Code by: Kevin Martin
*/

function zoom_image(baseImage,max_width,max_height,isSecure,baseImage2) {
	this.DynLayer = DynLayer;
	this.DynLayer();

	this.zoom_width  = max_width;
	this.zoom_height = max_height;

	this.tiles = Math.max(Math.ceil(this.zoom_factor),this.tiles);
	this.zoom_bar = new ts_ScrollBar(ts_ScrollBar.HORIZONTAL,0,0,false,null);
	this.zoom_bar.setVisible(false);

	//If 2 params then different background image
	this.baseImageF = baseImage;
	this.baseImageB = baseImage2 || baseImage;
	this.isSecure = false || isSecure;

	if( this.isSecure == true ) {
		this.imgF = new DynImage.getImage(this.baseImageF+this.pBreak(this.baseImageF)+'ZoomRGN');
		this.imgB = new DynImage.getImage(this.baseImageB+this.pBreak(this.baseImageB)+'ZoomBase');
	} else {
		this.imgF = new DynImage.getImage(this.baseImageF+this.pBreak(this.baseImageF)+'cell='+(max_width)+','+(max_height)+'&cvt=jpeg');
		this.imgB = new DynImage.getImage(this.baseImageB+this.pBreak(this.baseImageB)+'cell='+(max_width)+','+(max_height)+'&cvt=jpeg');
	};

	this.dragzone = new DynLayer(null,0,0,0,0,this.backgroundcolor);
	//this.dragzone.setVisible(false);
	this.msgLayer = new DynLayer(null,0,0,0,0,'#555555');
	this.imgCover = new DynLayer(null,0,0,0,0);

	this.bgimg  = new DynImage(this.imgB);
	this.dbimg  = new tsImage(this.imgF);
	this.dbimg.inTrans = 0;

	if( max_width*max_height > 150000 ) this.maxWait = 25000;

//	setTimeout( this+'.fetchBaseImages('+this.maxWait+');', 1000 ); //this.fetchBaseImages(this.maxWait);
};

zoom_image.prototype = new DynLayer;
zoom_image.prototype.debugMode = false;
zoom_image.prototype.maxWait = 15000;
zoom_image.prototype.delay = 20;
zoom_image.prototype.buttonType	= 0;
zoom_image.prototype.imgBorder = 0;
zoom_image.prototype.bgcolor = "#666666";
zoom_image.prototype.backgroundcolor = "#FFFFFF";
zoom_image.prototype.imgFolder = null;
zoom_image.prototype.buttonOffset = 0;
zoom_image.prototype.buttonSpace = 0;

zoom_image.prototype.initRGNZone = null;
zoom_image.prototype.RGNtype = "rgn";

zoom_image.prototype.initComp = new Array(false,false);
zoom_image.prototype.initComplete = false;
zoom_image.prototype.initWatch = true;

zoom_image.prototype.max_zoom = 5;
zoom_image.prototype.zoom_factor = 2;
zoom_image.prototype.totalzoom = 1;
zoom_image.prototype.tiles = 5;		//must be minimum zoom_factor
zoom_image.prototype.panPercision = 3;	//# of decimal places to use in RGN's

zoom_image.prototype.rgn_x = 0;
zoom_image.prototype.rgn_y = 0;
zoom_image.prototype.rgn_hei = 1;
zoom_image.prototype.infotxt = "";

zoom_image.prototype.MessageLayer = "Initializing Zoom...";
zoom_image.prototype.ZoomError = false;

zoom_image.prototype.img_width = -1;
zoom_image.prototype.img_height = -1;


//Offsets used when panning.  This is required because when we start dragging we need to know
//our origin since the drag event does not remember. (cumulitive)
zoom_image.prototype.bx=0;
zoom_image.prototype.by=0;
zoom_image.prototype.bw=0;
zoom_image.prototype.bh=0;
zoom_image.prototype.dx=0;
zoom_image.prototype.dy=0;


zoom_image.prototype.nav_w = 50;
zoom_image.prototype.nav_h = 50;
zoom_image.prototype.nav_x = 5;
zoom_image.prototype.nav_y = 5;
zoom_image.prototype.nav_pos = 0;

zoom_image.prototype.isdrag=false;

zoom_image.prototype.buf_size = 32; // overbuffer granularity
zoom_image.prototype.buf_off_x = 0;
zoom_image.prototype.buf_off_y = 0;


function compClip(c1,c2) {
	if( c1[0] == c2[0] && c1[1] == c2[1] && c1[2] == c2[2] && c1[3] == c2[3] ) return true;
	return false;
};

zoom_image.prototype.fetchBaseImages = function(mWait) {
	if( !(is.ns4 || is.ns5 || is.ns6 || is.ie4) ) {

		var chk_SecureSize = ((this.imgF.width!=this.zoom_width || this.imgF.height!=this.zoom_height)&&this.isSecure);
		var chk_Success = (!isMacIE() && (this.dbimg.getImage().complete && !compClip(this.dbimg.getClip(),[0,0,0,0])) && (this.bgimg.getImage().complete && !compClip(this.bgimg.getClip(),[0,0,0,0])) && this.imgF.width >=2 && this.imgB.width >=2 && this.bgimg.getImage().width >=2 && this.dbimg.getImage().width >=2 );
		var chk_SuccessNs6 = (is.ns6 && this.imgF.width >=2 && this.imgB.width >=2);
		var chk_SuccessMac = ( isMacIE() && this.bgimg.getWidth() >= 2 && this.dbimg.getWidth() >= 2 && this.dbimg.getImage().complete && this.bgimg.getImage().complete );

		if( mWait >= 0 && !(chk_Success || chk_SuccessNs6 || chk_SuccessMac) ) {

			this.dragzone.setVisible(false);
			setTimeout( this+'.fetchBaseImages('+eval(mWait-this.delay)+');', this.delay );
			return;
		};

		if( chk_SecureSize ) {
			//INCORRECT SIZE REQUEST FOR SECURE ZOOM
			this.MessageLayer = "Incorrect size for Secure Zoom.  The size must match the settings in the associated .IPS file.  See the Zoom Resource Guide for more information.";
			this.ZoomError = true;
			this.img_width = this.zoom_width;
			this.img_height = this.zoom_height;
		} else if( chk_Success || chk_SuccessNs6 ) {
			//SUCCESSFULLY LOADED IMAGE
			this.img_width  = this.imgF.width;
			this.img_height = this.imgF.height;
		} else if ( chk_SuccessMac ) {
			//SUCCESSFULLY LOADED ON MAC
			this.img_width = this.dbimg.getWidth();
			this.img_height = this.dbimg.getHeight();
		} else {
			//IMAGE FAILED TO LOAD
			this.MessageLayer = "Unable to load Image";

			this.ZoomError = true;
			this.img_width = this.zoom_width;
			this.img_height = this.zoom_height;
		};

		this.bw=this.img_width;
		this.bh=this.img_height;

		this.dbimg.set_size( this.img_width, this.img_height );

		this.initComp[0] = true;
	//	this.fixSize();
		setTimeout( this+'.fixSize();',100);
	};
};

function isMacIE() {
	if( is.platform=="mac" && (is.ie5 || is.ie4 ) ) return true;
	else return false;
};

zoom_image.prototype.initPageSettings = function(absolutePos) {
	var buttonSize = new Array(20,20);
	var waitSize = new Array(13,22);
	
	this.setSize(this.zoom_width+this.imgBorder*2,this.zoom_height+(this.imgFolder!=null?buttonSize[1]-Math.min(buttonSize[1],this.buttonOffset)+1:0)+this.imgBorder*2);

	var ui_frame = new DynLayer(null,0,0,this.zoom_width+this.imgBorder*2,this.zoom_height+(this.imgFolder!=null?buttonSize[1]-Math.min(buttonSize[1],this.buttonOffset):0)+this.imgBorder*2,this.bgcolor);
	this.addChild(ui_frame);

	this.addChild(this.msgLayer);
	this.msgLayer.setSize(this.zoom_width,18);
	this.msgLayer.moveTo(this.imgBorder,this.imgBorder+(this.zoom_height-this.msgLayer.h)/2);

	if( is.ns4 || is.ns5 || is.ns6 || is.ie4 ) {
		this.setmsgLayer( (is.ns ? navigator.userAgent.substring(navigator.userAgent.indexOf("Netscape")):navigator.appName +" " + navigator.appVersion) + " is not a supported browser version.  Please upgrade your browser in order to view this zoom window." );
	} else {
	this.setmsgLayer(this.MessageLayer);


	this.navbox = new ts_NavBox( this.imgB,this.nav_w,this.nav_h,this.backgroundcolor,this.bgcolor);

	//Note: the ie6CoverFix is to fix a problem with dragging the image under ie6 browsers
	//      the browsers require a full sized image to drag all other browsers allow an empty
	//      layer to be dragged.
	var ie6CoverFix = new tsImage( DynImage.getImage( getTSGif(DynAPI.librarypath+"tsapi/images/cover.tsgif"),this.zoom_width,this.zoom_height),"#" );
	this.imgCover.addChild(ie6CoverFix);
	this.imgCover.setSize(this.zoom_width, this.zoom_height);
	ie6CoverFix.setSize(this.zoom_width, this.zoom_height);

	this.dragzone.setBgColor(this.backgroundcolor);

	this.dragzone.moveTo(this.imgBorder,this.imgBorder);
	this.dragzone.setSize(this.zoom_width,this.zoom_height);
	this.addChild( this.dragzone );

	this.dragzone.addChild( this.bgimg );
	this.dragzone.addChild( this.dbimg );

	this.zoom_bar.setSize( Math.max((30+this.max_zoom),Math.round((this.zoom_width-100)/2)) ,13 );

	if( this.nav_pos >= 1 && this.nav_pos <= 4 ) {
		DragEvent.enableDragEvents(this.navbox);
		this.nav_x = 5; this.nav_y = 5;
		if( this.nav_pos == 2 || this.nav_pos == 3 ) this.nav_x = this.zoom_width-this.nav_w-5; 
		if( this.nav_pos == 3 || this.nav_pos == 4 ) this.nav_y = this.zoom_height-this.nav_h-5;
		this.navbox.moveTo(this.nav_x,this.nav_y);
		this.navbox.setVisible(false);
		this.addChild( this.navbox );
	};

	this.hourglass = new DynImage( DynImage.getImage(getTSGif(DynAPI.librarypath+"tsapi/images/wait.tsgif"),waitSize[0],waitSize[1]) );
	this.hourglass.moveTo(this.zoom_width-waitSize[0]-5+this.imgBorder,5+this.imgBorder);
	this.hourglass.setSize(waitSize[0],waitSize[1]);
	this.hourglass.setVisible(false);
	this.addChild( this.hourglass );

	this.hourglass2 = new DynLayer(null,0,0,this.zoom_width-10,12);
	this.hourglass2.setVisible(false);
	this.addChild( this.hourglass2 );
	this.hourglass2.moveTo(5+this.imgBorder,this.zoom_height-12+this.imgBorder);

	this.imgCover.moveTo(0,0);
	this.imgCover.setSize( this.zoom_width, this.zoom_height );
	this.dragzone.addChild( this.imgCover );

	if( is.ns && !is.ns4 ) this.imgCover.setZIndex(1);

	DragEvent.dragPlay = 1; // allow a 1 pixel mouse move without invoking drag

	if( this.imgFolder != null ) {
		var infoShift = (this.infotxt!=""?1:0);
		var plusbutton = new tsImage( DynImage.getImage(getTSGif(this.imgFolder+'zoomin.tsgif')) ,"javascript:"+this+".ZoomIn("+Math.round(this.zoom_width/2)+","+Math.round(this.zoom_height/2)+");");
		plusbutton.moveTo( this.zoom_width-1-(3+infoShift)*(buttonSize[0]+this.buttonSpace)+this.imgBorder,this.zoom_height-this.buttonOffset+this.imgBorder);
		plusbutton.setSize(buttonSize[0],buttonSize[1]);
		this.addChild(plusbutton);


		var minusbutton = new tsImage( DynImage.getImage(getTSGif(this.imgFolder+'zoomout.tsgif')) ,"javascript:"+this+".ZoomOut()");
		minusbutton.moveTo( this.zoom_width-1-(2+infoShift)*(buttonSize[0]+this.buttonSpace)+this.imgBorder,this.zoom_height-this.buttonOffset+this.imgBorder);
		minusbutton.setSize(buttonSize[0],buttonSize[1]);
		this.addChild(minusbutton);
	 
		var resetbutton = new tsImage( DynImage.getImage(getTSGif(this.imgFolder+'reset.tsgif')) ,"javascript:"+this+".ZoomReset()");
		resetbutton.moveTo( this.zoom_width-1-(1+infoShift)*(buttonSize[0]+this.buttonSpace)+this.imgBorder,this.zoom_height-this.buttonOffset+this.imgBorder);
		resetbutton.setSize(buttonSize[0],buttonSize[1]);
		this.addChild(resetbutton);

		if( this.infotxt != "" ) {
			var infobutton = new tsImage( DynImage.getImage(getTSGif(this.imgFolder+'info.tsgif')) ,"javascript:"+this+".showInformation()");
			infobutton.moveTo( this.zoom_width-1-infoShift*(buttonSize[0]+this.buttonSpace)+this.imgBorder,this.zoom_height-this.buttonOffset+this.imgBorder);
			infobutton.setSize(buttonSize[0],buttonSize[1]);
			this.addChild(infobutton);
		};
	};

	var ClickListener = new EventListener(this);
	ClickListener.key="";
	ClickListener.isdrag = false;
	ClickListener.ondragmove = function(e) { 
		var o = e.getTarget();
		this.isdrag=true;
		o.imgCover.setVisible(false);

		if( (o.buttonType == 1 && this.key.indexOf('s') == -1) || (o.buttonType == 0 && this.key.indexOf('s') >= 0) ) {
			o.invokeEvent('panSpecial'); 
		} else {
			o.invokeEvent('pan',e,e);
		};
	};
	ClickListener.onmousedown = function(e) {
		this.key = "";
		if( e.shiftKey ) this.key += "s";
		if( e.ctrlKey ) this.key += "c";
		if( e.altKey ) this.key += "a";
	};

	ClickListener.onmouseup = function(e) { 
		var o = e.getTarget();
		e.parent = o;
		if( this.isdrag==false ) {
			this.isdrag = false;
			if (e.ctrlKey || e.shiftKey) 			{ o.invokeEvent('zoomout'); }
			else if (e.altKey) 				{ o.invokeEvent('zoomreset'); }
			else						{ o.invokeEvent('zoomin',e); };
		} else {
			this.isdrag = false;
                };
	};
	ClickListener.ondragend=function(e) { 
		var o = e.getTarget();
		o.imgCover.setVisible(true);

		if( (o.buttonType == 1 && this.key.indexOf('s') == -1) || (o.buttonType == 0 && this.key.indexOf('s') >= 0) ) {
			o.imgCover.moveTo(0,0);
			o.invokeEvent('panDoneSpecial');
		} else {
			o.invokeEvent('panDone');
		}; 
	};
	this.imgCover.addEventListener(ClickListener);

	//LISTENER FOR NAV WINDOW
	var ClickListener2 = new EventListener(this);
	ClickListener2.key=""; ClickListener2.px = 0; ClickListener2.py = 0;
	ClickListener2.onmousedown = function(e) {
		this.key = "";	this.px = e.x; this.py = e.y;
		if( e.shiftKey ) this.key += "s";
		if( e.ctrlKey ) this.key += "c";
		if( e.altKey ) this.key += "a";
	};
	ClickListener2.onmouseup = function(e) {
		var o = e.getTarget();
		if( this.px==e.x && this.py==e.y ) {
			if (e.ctrlKey || e.shiftKey) 	o.invokeEvent('zoomout');
			else if (e.altKey)		o.invokeEvent('zoomreset');
			else				o.invokeEvent('zoomin_nav',e);
		} else {		//dragged
			o.invokeEvent('panDone');	
 		};
	};
	ClickListener2.ondragmove = function(e) { var o = e.getTarget();
		var x = (o.bw<=o.zoom_width?0: Math.min( -o.bx, Math.max( o.zoom_width-o.bw-o.bx, (o.nav_x-o.navbox.x)/o.nav_w*o.bw )) );	
		var y = (o.bh<=o.zoom_height?0: Math.min( -o.by, Math.max( o.zoom_height-o.bh-o.by, (o.nav_y-o.navbox.y)/o.nav_h*o.bh )) );

		o.navbox.moveTo(o.nav_x,o.nav_y);
		o.dbimg.moveTo( (o.bw<=o.zoom_width?o.bx:o.dx+x), (o.bh<=o.zoom_height?o.by:o.dy+y) ); 
		o.bgimg.moveTo( o.bx+x, o.by+y );
		o.navbox.rgnshift( o.bx+x,o.by+y,o.bw,o.bh );
	};

	this.navbox.addEventListener(ClickListener2);
	this.navbox.setZIndex(1);

	//Top LevelListener
	var vlistener = new EventListener(this);
	vlistener.onzoomreset = function(e) 	{ var o = e.getTarget(); o.ZoomReset(); };
	vlistener.onzoomout = function(e)	{ var o = e.getTarget(); o.ZoomOut(); };
	vlistener.onzoomin = function(e)	{ var o = e.getTarget(); 
		if( is.ns ) o.ZoomIn(e.x+o.dbimg.x,e.y+o.dbimg.y); 
		else	    o.ZoomIn(event.offsetX+o.dbimg.x, event.offsetY+o.dbimg.y);
	};
	vlistener.onzoomin_nav = function(e) 	{ var o = e.getTarget();
		if( is.ns ) o.ZoomIn(e.x/o.nav_w*o.bw+o.bx+o.dbimg.x, e.y/o.nav_h*o.bh+o.by+o.dbimg.y);
		else	    o.ZoomIn(event.offsetX/o.nav_w*o.bw+o.bx+o.dbimg.x, event.offsetY/o.nav_h*o.bh+o.by+o.dbimg.y);
	};
	vlistener.onpanDone = function(e)	{ 
		var o = e.getTarget(); 

		o.bx = o.bgimg.x; 
		o.by = o.bgimg.y; 

		o.Pan(o.imgCover.x,o.imgCover.y); 

		o.dbimg.inTrans = 0; 
	};
        vlistener.onpan = function(e,e2) {
		var o = e.getTarget();

		o.dbimg.inTrans = -1;		//Remove wait icon and stop background move

		if( o.bw != o.img_width || o.bh != o.img_height ) {
			o.fixImageSize( o.bgimg,o.bw,o.bh,false );

			var x1 = Math.min( 0-o.bx, Math.max( o.img_width -o.bw-o.bx, o.imgCover.x ) );
			var y1 = Math.min( 0-o.by, Math.max( o.img_height-o.bh-o.by, o.imgCover.y ) );

			//SQUARE OFF FIXES
			if( o.bh+o.by+y1<o.zoom_height && o.bh >= o.zoom_height ) y1 = o.zoom_height-o.bh-o.by; 
			if( o.bw+o.bx+x1<o.zoom_width && o.bw >= o.zoom_width ) x1 = o.zoom_width-o.bw-o.bx;
			if( o.bh <= o.zoom_height ) y1 = -o.by;
			if( o.bw <= o.zoom_width ) x1 = -o.bx;

			o.imgCover.moveTo(x1,y1);
			o.dbimg.moveTo( (o.bw<o.zoom_width-2?(o.zoom_width-o.bw)/2:o.dx+x1),(o.bh<o.zoom_height-2?(o.zoom_height-o.bh)/2:o.dy+y1) );
	               	o.bgimg.moveTo( o.bx+x1+Math.max(0,(o.zoom_width-o.bw)/2), o.by+y1+Math.max(0,(o.zoom_height-o.bh)/2) );


			o.navbox.rgnshift( o.bx+x1+Math.max(0,(o.zoom_width-o.bw)/2),o.by+y1+Math.max(0,(o.zoom_height-o.bh)/2),o.bw,o.bh );

		};
	};
	this.addEventListener(vlistener);

//KEVIN

	var waitListener = new EventListener(this);
	waitListener.onwaitOn  = function(e) { e.getTarget().hourglass.setVisible(true); e.setBubble(false); };
	waitListener.onwaitOff = function(e) { e.getTarget().hourglass.setVisible(false); e.setBubble(false); };
	this.dbimg.addEventListener(waitListener);

	var imgSpecialListener = new EventListener(this.dbimg);
	imgSpecialListener.onloadSpecial = function(e) {
		var o = e.getTarget();
		o.setVisible(false);
		o.invokeEvent("waitOn");
	};
	imgSpecialListener.ondoneSpecial = function(e) {
		var o = e.getTarget();
		if( o.inTrans <= 0 ) { 
			if( o.inTrans == 0 ) o.setVisible(true);
			o.invokeEvent("waitOff");
		} else {
			setTimeout( o+'.invokeEvent("doneSpecial");',50 );
			return;
		};
	};
	this.dbimg.addEventListener(imgSpecialListener);

	var SlideListener = new EventListener(this);
	SlideListener.onscrollStop = function(e) { 
		var o = e.getTarget(); 

		o.bx = o.bgimg.x;
		o.by = o.bgimg.y;	//need to set o.bw and o.bh as well

		o.ResetRegion(o.bgimg.x,o.bgimg.y,o.bw,o.bh);
		e.setBubble(false);
	};
	SlideListener.onscroll = function(e)     { 
		var o = e.getTarget(); 

		var new_z = Math.pow( o.zoom_factor, (o.zoom_bar.getPcnt()*o.max_zoom) );
		var new_h = o.img_height*new_z;
		var new_w = o.img_width*new_z;

		var new_y = Math.min(0, Math.max( o.img_height-new_h, o.img_height/2-(o.img_height/2-o.by)/o.bh*new_h) );
		var new_x = Math.min(0, Math.max( o.img_width-new_w,  o.img_width/2 -(o.img_width/2-o.bx)/o.bw *new_w) );

		//SQUARE OFF FIXES
		if( new_h+new_y<o.zoom_height) new_y = (o.zoom_height-new_h)/2; 
		if( new_w+new_x<o.zoom_width)  new_x = (o.zoom_width-new_w)/2;

		o.dbimg.setVisible(false);
		o.bgimg.moveTo(new_x,new_y);
		o.bgimg.setSize( new_w,new_h );
		e.setBubble(false);
	}; 
	this.zoom_bar.addEventListener(SlideListener);

        this.addChild(this.zoom_bar);
        this.zoom_bar.moveTo(this.imgBorder+this.buttonSpace,this.zoom_height+this.imgBorder-this.buttonOffset+buttonSize[1]/2-4);

//KEVIN

	DragEvent.enableDragEvents(this.imgCover);
	this.invokeEvent('resizedone');


	if( this.debugMode == true ) {
		this.debugWindow = new DynLayer( null,0,0,300,50,"F0F0F0");
		this.debugWindow.setVisible(true);
	        this.addChild( this.debugWindow );
	};


	this.imgCover.setSize(this.zoom_width, this.zoom_height);
	ie6CoverFix.setSize(this.zoom_width, this.zoom_height);
	ie6CoverFix.setClip( new Array(0,this.zoom_width,this.zoom_height,0) );

//	this.fixSize();

	};


	if( ""+absolutePos=="true" ) 	{ DynAPI.document.addChild(this.parent); }
	else				{ DynAPI.document.addInflowChild(this.parent); };

	this.initComp[1] = true;

};

zoom_image.prototype.fixSize = function() {
	if( !this.initComp[0] || !this.initComp[1] ) {
		setTimeout( this+'.fixSize();',100);
		return;
	};
	if( this.initComp[0] && this.initComp[1] ) {
		if( !this.ZoomError ) {
			this.dbimg.setSize( this.img_width, this.img_height );    
			this.bgimg.setSize( this.img_width, this.img_height );    
			this.bgimg.moveTo( Math.max(0,(this.zoom_width-this.img_width)/2),Math.max(0,(this.zoom_height-this.img_height)/2) );
			this.dbimg.moveTo( Math.max(0,(this.zoom_width-this.img_width)/2),Math.max(0,(this.zoom_height-this.img_height)/2) );

			this.bgimg.setVisible(true);
			this.dbimg.setVisible(true);
			this.dragzone.setVisible(true);

			this.navbox.setNavImage( this.imgB, Math.max(0,(this.zoom_width-this.img_width)/2),Math.max(0,(this.zoom_height-this.img_height)/2), this.img_width, this.img_height );
			this.navbox.setVisible(true);
		};
		this.initComplete = true;
	};
};

zoom_image.prototype.debug = function(str,doAppend) {
	if( this.debugMode == true ) {
		if( ""+doAppend != "true" ) doAppend = false;
		var old = this.debugWindow.getHTML();
		this.debugWindow.setHTML( (doAppend==true&&old!=null?old:"")+"<font size=1 face=arial>"+str+"</font>");
	};
};

zoom_image.prototype.pBreak = function(sImg) {
	return( (sImg.indexOf("?")>=0?"&":"?") );
};

zoom_image.prototype.ZoomReset = function() {
   if( this.initComplete && !this.ZoomError ) {
//      this.ResetBackground(Math.max(0,(this.zoom_width-this.img_width)/2),Math.max(0,(this.zoom_height-this.img_height)/2),this.img_width,this.img_height);
      if( this.initRGNZone == null ) {
         this.zoom_bar.reset();
	 //         this.ResetRegion();
	 this.ZoomSet( "0,0," + this.img_width/this.img_height + ",1", this.RGNtype );
      } else {
         this.ZoomSet( this.initRGNZone,this.RGNtype );
      };
   };
};

zoom_image.prototype.setButtonType = function(buttonType) {
	this.buttonType = buttonType || 0;
};

zoom_image.prototype.enableZoomBar = function() {
	if( this.imgFolder != null ) this.zoom_bar.setVisible(true);
};

zoom_image.prototype.disableZoomBar = function() {
	this.zoom_bar.setVisible(false);
};

zoom_image.prototype.ResetRegion = function(bgx,bgy,bgw,bgh,isInitial) {
  if( !this.ZoomError ) {
    if( isInitial!=true ) this.initWatch = false;	//stop watching for unkown size reset

    this.dx = 0;
    this.dy = 0;

    var bufx, bufy, bufw, bufh, bufbgw, bufbgh;
    if( this.isSecure==true )
    {
      bufx = 0 - bgx;
      bufy = 0 - bgy;
      bufw = this.zoom_width;
      bufh = this.zoom_height;
      bufbgw = bgw;
      bufbgh = bgh;
      this.buf_off_x = 0;
      this.buf_off_y = 0;
    }
    else
    {
       if( bgw <= this.zoom_width )
      {
	bufx = 0 - bgx;
	bufw = this.zoom_width;
	bufbgw = bgw;
	this.buf_off_x = 0;
      } else
      {
	bufx = this.buf_size*Math.floor((0-bgx)/this.buf_size);
	bufw = this.buf_size*Math.ceil((this.zoom_width - bgx)/this.buf_size) - bufx;
	bufbgw = this.buf_size*Math.ceil((bufx + bgw)/this.buf_size) - bufx;
	this.buf_off_x = bufx + bgx;
      }
      if( bgh <= this.zoom_height )
      {
	bufy = 0 - bgy;
	bufh = this.zoom_height;
	bufbgh = bgh;
	this.buf_off_y = 0;
      }
      else
      {
	bufy = this.buf_size*Math.floor((0-bgy)/this.buf_size);
	bufh = this.buf_size*Math.ceil((this.zoom_height - bgy)/this.buf_size) - bufy;
	bufbgh = this.buf_size*Math.ceil((bufy + bgh)/this.buf_size) - bufy;
	this.buf_off_y = bufy + bgy;
      }
    }

    this.imgCover.moveTo(0,0);

    var RGNx = rounder( (bgw < this.zoom_width?0:Math.max(0,bufx)) /bgh, this.panPercision);
    var RGNy = rounder( (bgh < this.zoom_height?0:Math.max(0,bufy)) /bgh, this.panPercision);
    var RGNw = rounder( (bgw < this.zoom_width?bgw:bufw) /bgh, this.panPercision);
    var RGNh = rounder( (bgh < this.zoom_height?bgh:bufh) /bgh, this.panPercision);

    var RGN = (bgw==this.img_width?"":"rgn="+RGNx+","+RGNy+","+RGNw+","+RGNh+"&");

    var str = this.dbimg.getImageSrc();

    var w = bufw; 
    var h = bufh;
    if( bgw <= this.zoom_width ) w = bufbgw;
    if( bgh <= this.zoom_height ) h = bufbgh;

    if( this.isSecure==true )	var new_str = str.substr( 0, (str.indexOf("rgn")>0?str.indexOf("rgn="):str.indexOf("ZoomRGN")) )+RGN+"ZoomRGN";
    else			var new_str = str.substr( 0, str.indexOf("cell=") )+ "cell=" + w +"," + h + "&"  +RGN+"cvt=jpeg";

    //if in same tile then do nothing
    if( str != new_str ) {


      this.baseImageF = new_str;

      this.dbimg.setVisible(false);

      this.dbimg.setSize(w,h);
      this.dbimg.set_size(w,h);
      // jerry comment: avoid the image rollback after zoom in
      //this.dbimg.setImageSpecial( this.baseImageF );
      this.dbimg.moveTo(Math.max(0,(this.zoom_width-bgw)/2) + this.buf_off_x,Math.max(0,(this.zoom_height-bgh)/2) + this.buf_off_y);

      this.dx = Math.max(0,(this.zoom_width-bgw)/2) + this.buf_off_x;
      this.dy = Math.max(0,(this.zoom_height-bgh)/2) + this.buf_off_y;

    } else {
	this.dbimg.setVisible(true);		//need to do this regardless of str!=new_str for zoombar
    };
  };
};

zoom_image.prototype.ResetBackground = function(x,y,w,h) {
	this.dbimg.inTrans++;
	this.dbimg.setVisible(false);
	setTimeout( this+'.resetBGFade('+this.bgimg.x+','+this.bgimg.y+','+this.bw+','+this.bh+','+x+','+y+','+w+','+h+',0,20,'+eval(this.dbimg.inTrans)+');', 20 );
};

zoom_image.prototype.resetBGFade = function( ox,oy,ow,oh,x,y,w,h,i,iloops,tn ) {
	if( tn >= this.dbimg.inTrans && this.dbimg.inTrans > 0 ) {
		if( i < iloops ) {
			i++;
			this.bgimg.setSize( ow + (w-ow)*(i/iloops),oh + (h-oh)*(i/iloops) );
			this.bgimg.moveTo ( ox + (x-ox)*(i/iloops),oy + (y-oy)*(i/iloops) );

			this.bw = ow + (w-ow)*(i/iloops);
			this.bh = oh + (h-oh)*(i/iloops);
			this.bx = ox + (x-ox)*(i/iloops);
			this.by = oy + (y-oy)*(i/iloops);
	
			this.navbox.rgnshift( this.bx,this.by,this.bw,this.bh );

			setTimeout( this+'.resetBGFade('+ox+','+oy+','+ow+','+oh+','+x+','+y+','+w+','+h+','+i+','+iloops+','+tn+');', 30 );
			return;
		} else {
			this.dbimg.inTrans = 0;
		};
	};
};


zoom_image.prototype.ZoomInit = function(rgn,type) {
  if( type == "rgnn" )	this.RGNtype = "rgnn";
  this.initRGNZone = rgn;
};

zoom_image.prototype.ZoomSet = function( rgn, type, isInitial ) {
  if( this.initComplete==true && !this.ZoomError ) {

     this.fixImageSize(this.bgimg,this.bw,this.bh);

     var rgnArray = rgn.split(",");

     if( rgnArray.length == 4 ) {

        var aspect = this.img_width/this.img_height;
        if( type=="rgnn" ) rgnArray[2] = rounder(rgnArray[3]*aspect, this.panPercision);

        var new_z = Math.min(1,Math.max( this.img_height/(this.img_height*Math.pow(this.zoom_factor,this.max_zoom)) ,parseFloat(rgnArray[3])));

        var new_h = parseInt(this.img_height/new_z );
        var new_w = parseInt(this.img_width/new_z );
        var new_x = parseInt(0-new_h* Math.max(0,Math.min( (1-new_z)*aspect,rgnArray[0]) ) );
        var new_y = parseInt(0-new_h* Math.max(0,Math.min( 1-new_z,rgnArray[1]) ) );

	//SQUARE OFF FIXES
	if( new_h+new_y<this.zoom_height) new_y = Math.min(0,this.zoom_height-new_h); 
	if( new_w+new_x<this.zoom_width) new_x = Math.min(0,this.zoom_width-new_w);

        this.zoom_bar.setPos( this.zoom_bar.getMaxPos()*( custLog(new_h/this.img_height,this.zoom_factor)/this.max_zoom ) );

	new_x = new_x+Math.max(0,(this.zoom_width-new_w)/2);
	new_y = new_y+Math.max(0,(this.zoom_height-new_h)/2);
	this.ResetBackground(new_x,new_y,new_w,new_h);
        this.ResetRegion( new_x,new_y,new_w,new_h,isInitial  );
     };
   };
};


zoom_image.prototype.ZoomOut = function() {
  this.fixImageSize(this.bgimg,this.bw,this.bh);

  if( this.initComplete && this.bgimg.w > this.img_width && !this.ZoomError ) {
     var actualMove = Math.min( this.zoom_bar.getPos()-0, (this.zoom_bar.getMaxPos()/this.max_zoom) );
     this.zoom_bar.setPos( this.zoom_bar.getPos()-actualMove );

     var new_z = Math.pow( this.zoom_factor, (this.zoom_bar.getPcnt()*this.max_zoom) );
     var new_h = this.img_height*new_z;
     var new_w = this.img_width*new_z;
     var new_y = Math.min(0, Math.max( this.img_height-new_h, this.img_height/2+new_h*(this.bgimg.y-this.img_height/2)/this.bgimg.h ) );
     var new_x = Math.min(0, Math.max( this.img_width-new_w, this.img_width/2+new_w*(this.bgimg.x-this.img_width/2)/this.bgimg.w ) );

     //SQUARE OFF FIXES
     if( new_h+new_y<this.zoom_height) new_y = Math.min(0,this.zoom_height-new_h);
     if( new_w+new_x<this.zoom_width) new_x = Math.min(0,this.zoom_width-new_w);

     this.blockZoomCoords( new_x,new_y,new_w,new_h );
  };
};



zoom_image.prototype.ZoomIn = function(ctr_x, ctr_y) {
  this.fixImageSize(this.bgimg,this.bw,this.bh);

  if( this.initComplete && this.bgimg.h < this.img_height*Math.pow(this.zoom_factor,this.max_zoom) && !this.ZoomError ) {
     var actualMove = Math.min( (this.zoom_bar.getMaxPos()-this.zoom_bar.getPos()), (this.zoom_bar.getMaxPos()/this.max_zoom) );
     this.zoom_bar.setPos( this.zoom_bar.getPos()+actualMove );

     var new_z = Math.pow( this.zoom_factor, (this.zoom_bar.getPcnt()*this.max_zoom) );
     var new_h = this.img_height*new_z;
     var new_w = this.img_width*new_z;
     var new_y = Math.min( 0, Math.max( this.img_height-new_h, this.img_height/2-(ctr_y-this.bgimg.y)*(new_h/this.bgimg.h) ) );
     var new_x = Math.min( 0, Math.max( this.img_width -new_w, this.img_width/2-(ctr_x-this.bgimg.x)*(new_w/this.bgimg.w) ) );
     //SQUARE OFF FIXES
     if( new_h+new_y<this.zoom_height) new_y = Math.min(0,this.zoom_height-new_h);
     if( new_w+new_x<this.zoom_width) new_x = Math.min(0,this.zoom_width-new_w);
     this.blockZoomCoords( new_x,new_y,new_w,new_h );
  };
};

function custLog(x,base) { return (Math.log(x)/Math.log(base)); }
function rounder(f,p) {	return Math.round(f*Math.pow(10,p))/Math.pow(10,p); };

zoom_image.prototype.blockZoomCoords = function( x,y,w,h ) {

  //SQUARE OFF FIXES
  if( h > this.img_height || w > this.img_width ) {
     var curLevel = Math.round(custLog(h/this.img_height,this.zoom_factor)*100)/100;
     var tilesize = new Array ( w/(curLevel*this.tiles), h/(curLevel*this.tiles) );

     x = Math.min(0, Math.max(this.zoom_width-w, Math.round(Math.round((0-x)/tilesize[0])*tilesize[0]*-1) ));
     y = Math.min(0, Math.max(this.zoom_height-h,Math.round(Math.round((0-y)/tilesize[1])*tilesize[1]*-1) ));
  };

  x = x+Math.max(0,(this.zoom_width-w)/2);
  y = y+Math.max(0,(this.zoom_height-h)/2);

  this.ResetBackground(x,y,w,h);
  this.ResetRegion(x,y,w,h);
};

zoom_image.prototype.Pan = function(diff_x, diff_y) {
  if( this.initComplete && !this.ZoomError ) { 
     this.fixImageSize(this.bgimg,this.bw,this.bh,false);
     this.ResetRegion(this.bgimg.x,this.bgimg.y,this.bw,this.bh); 
  };
};


zoom_image.prototype.showInformation = function() {
	if( this.infotxt.length != 0 )	{ eval(this.infotxt); }  
	else				{ window.open( DynAPI.librarypath+'tsapi/helppage.html','DHTMLZoomInfo','width=300,height=400' ); };  
};


zoom_image.prototype.addInformation = function(str) {
	this.infotxt = "alert('"+str+"')";
};

zoom_image.prototype.setHelpPage = function(str,w,h) {
	if( ""+str == "undefined" )  	this.infotxt = "window.open('"+DynAPI.librarypath+"tsapi/helppage.html','DHTMLZoomInfo','width="+w+",height="+h+"')";
	else					this.infotxt = "window.open('"+str+"','DHTMLZoomInfo','width="+w+",height="+h+"')";
};

zoom_image.prototype.enableWaitIcon = function(str) {
	var str2 = str || "Please Wait...";
	this.hourglass2.setHTML("<font size=1 face=arial color=#000000>"+str2+"</font>");
	this.hourglass2.setVisible(true);
};

zoom_image.prototype.setmsgLayer = function(str) {
	var str2 = str || "";
	this.msgLayer.setSize( this.zoom_width, this.zoom_height );
	this.msgLayer.moveTo(this.imgBorder,this.imgBorder);
	this.msgLayer.setHTML("<table width="+eval(parseInt(this.zoom_width)-4)+" height="+eval(parseInt(this.zoom_height)-4)+" border=0 cellpadding=0 cellspacing=0><tr><td align=center valign=center><font size=2 face=arial color=#FFFFFF><b>"+str2+"</b></font></td></tr></table>");
	this.msgLayer.setVisible(eval(str2!=""));
};

zoom_image.prototype.disableWaitIcon = function() {
	this.hourglass2.setVisible(false);
};

zoom_image.prototype.setBackground = function(bgcolor) {
	if( ""+bgcolor=="undefined" ) bgcolor="#FFFFFF";
	this.backgroundcolor = "" + bgcolor || "#FFFFFF";
	if( this.backgroundcolor.substring(0,1) != "#" ) this.backgroundcolor="#"+this.backgroundcolor;
};

zoom_image.prototype.setBorder = function(sz,bgcolor) {
	if( ""+bgcolor=="undefined" ) bgcolor="#666666";
	this.bgcolor = "" + bgcolor || "#666666";
	this.imgBorder = sz || 0;
	if( this.bgcolor.substring(0,1) != "#" ) this.bgcolor="#"+this.bgcolor;
};

zoom_image.prototype.enableUI = function(imgFolder,offset,spacing) {
	if( ""+this.imgFolder == "undefined" ) this.imgFolder= null;
	this.imgFolder	= imgFolder || DynAPI.librarypath+"tsapi/images/default/";
	if( this.imgFolder.substring(this.imgFolder.length-1,this.imgFolder.length) != "/" ) this.imgFolder+="/";
	if( ""+offset == "undefined" ) offset=0;
	if( ""+spacing == "undefined" ) spacing=0;

	this.buttonOffset = 0 || offset;
	this.buttonSpace = 0 || spacing;
	this.zoom_bar.changeUI(this.imgFolder);
};

zoom_image.prototype.setMaxZoom = function(numZoomLevels, amount) {
	if( ""+amount == "undefined" || amount==null ) amount=2;
	if( amount <= 1 ) amount = 1.1;
	if( amount > 3 ) amount = 3;
	this.zoom_factor = amount;
	this.max_zoom = numZoomLevels;
};

zoom_image.prototype.enableNav = function(position) {
	this.nav_pos = position;
};

zoom_image.prototype.fixImageSize = function(obj,w,h,doResize) {		//KEIVN-NEW (fixes clip problems seen in Netscape)
	obj.w = w; obj.h = h;
	if( obj.getClip() != "0,"+w+","+h+",0")	{
		if( ""+doResize!="false" ) obj.setSize(w,h);
		obj.setClip( new Array(0,w,h,0) );
	};
};

zoom_image.prototype.manualPan = function(direction,amount) {
	if( this.initComplete && !this.ZoomError ) {
		if( ""+amount == "undefined" || amount==null ) amount=1;
		amount = Math.round(amount);

		this.fixImageSize(this.bgimg,this.bw,this.bh);

		var new_x = this.bgimg.x;
		var new_y = this.bgimg.y;

		//SQUARE OFF FIXES
	        var curLevel = Math.round(custLog(this.bgimg.h/this.img_height,this.zoom_factor)*100)/100;

		if( curLevel != 0 ) {
			var tilesize = new Array ( this.bgimg.w/(curLevel*this.tiles), this.bgimg.h/(curLevel*this.tiles) );
	
			if( direction.toUpperCase().indexOf("UP")>=0 )	 	{ new_y = Math.round(Math.min( Math.round(new_y/tilesize[1]+amount)*tilesize[1],0)); }
			else if( direction.toUpperCase().indexOf("DOWN")>=0 )	{ new_y = Math.round(Math.max( Math.round(new_y/tilesize[1]-amount)*tilesize[1],this.zoom_height-this.bgimg.h)); };
			if( direction.toUpperCase().indexOf("LEFT")>=0 )	{ new_x = Math.round(Math.min( Math.round(new_x/tilesize[0]+amount)*tilesize[0],0)); }
			else if( direction.toUpperCase().indexOf("RIGHT")>=0 )	{ new_x = Math.round(Math.max( Math.round(new_x/tilesize[0]-amount)*tilesize[0],this.zoom_width-this.bgimg.w)); };
			//this.bgimg.moveTo( (this.bw<this.zoom_width?this.bgimg.x:new_x), (this.bh<this.zoom_height?this.bgimg.y:new_y) );
			//this.Pan(0,0);
			this.fixImageSize(this.bgimg,this.bw,this.bh,false);
			var x = (this.bw<this.zoom_width?this.bgimg.x:new_x); 
			var y = (this.bh<this.zoom_height?this.bgimg.y:new_y); 
			var w = this.bw; 
			var h = this.bh;
			this.ResetBackground(x,y,w,h);
			this.ResetRegion(x,y,w,h); 
		};
	};
};

zoom_image.prototype.showImages = function() {
	if( !this.ZoomError ) {
		this.showImages2(this.maxWait);
	} else {
		this.setmsgLayer(this.MessageLayer);
	};
};

zoom_image.prototype.showImages2 = function(mWait) {
		if( this.dbimg.k_changing != 0 || !this.dbimg.getImage().complete || !this.bgimg.getImage().complete || this.bgimg.w <= 0 || this.dbimg.w <= 0 ) {
			if( mWait >= 0 ) {
				setTimeout( this+'.showImages2('+eval(mWait-this.delay)+');', this.delay );
				return;
			} else {
				this.setmsgLayer("Unable to load Region");
			}
		};
		this.setmsgLayer("");

		//this.bgimg.moveTo( Math.max(0,(this.zoom_width-this.bw)/2),Math.max(0,(this.zoom_height-this.bh)/2) );
		//this.dbimg.moveTo( Math.max(0,(this.zoom_width-this.dbimg.img_width)/2),Math.max(0,(this.zoom_height-this.dbimg.img_height)/2) );

		this.bgimg.setSize(this.bw,this.bh);
		this.dbimg.setSize(this.dbimg.img_width,this.dbimg.img_height);

		this.fixImageSize(this.bgimg,this.bw,this.bh,true);
		this.fixImageSize(this.dbimg,this.dbimg.img_width,this.dbimg.img_height,true);

		this.dragzone.setVisible(true);
		setTimeout( this+'.watchSize(20);',100);
};

//*******************
//** Fixes unknown problem where image size reverts back on init
zoom_image.prototype.watchSize = function(i) {
	if( i <= 0 && this.initWatch == false ) return;
	if( this.dbimg.img_height == this.dbimg.h ) {
		setTimeout( this+'.watchSize('+eval(i-1)+');',100);
		return;
	}
	this.bgimg.setSize(this.bw,this.bh);
	this.dbimg.setSize(this.dbimg.img_width,this.dbimg.img_height);
};


zoom_image.prototype.advanced = function( zoomAmount,tiles,percision ) {
	this.zoom_factor = Math.max(1.1,zoomAmount);
	this.tiles = Math.ceil(Math.max(this.zoom_factor,tiles));
	this.panPercision = Math.max(1,parseInt(percision));
};

function getTSGif(s) {
  return s;
};
